package com.diqye.db.datasources;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

import javax.sql.DataSource;

import com.diqye.db.exception.XAssert;
import com.diqye.templet.util.Strings;

public class SimpleDatasource implements DataSource {
	//private String url = "jdbc:mysql://127.0.0.1:3306/project?useUnicode=true&characterEncoding=utf8";
	//private String driverclass = "com.mysql.jdbc.Driver";
	public static boolean inited=false;
	private static String url = "jdbc:sqlite:C:/Users/IBM_ADMIN/Documents/db/diqye.db";
	private static String driverclass = "org.sqlite.JDBC";
	private static String username ="root";
	private static String password ="root";
	public SimpleDatasource(){
		if(inited)return;
		com.diqye.log.Logger log = com.diqye.log.Logger.getLogger(getClass());
		if("mopass".equals(System.getenv("diqye"))){ //如果是mopass环境
			log.info("初始化 mopass环境 数据库");
			String ip = System.getenv("MOPAAS_MYSQL14931_HOST");
			String port = System.getenv("MOPAAS_MYSQL14931_PORT");
			String databasename = System.getenv("MOPAAS_MYSQL14931_NAME");
			url = Strings.format("jdbc:mysql://{0}:{1}/{2}?useUnicode=true&characterEncoding=utf8",ip,port,databasename);
			driverclass= "com.mysql.jdbc.Driver";
			username=System.getenv("MOPAAS_MYSQL14931_USERNAME");
			password =System.getenv("MOPAAS_MYSQL14931_PASSWORD");
		}
		
		if("mysql".equals(System.getenv("diqye"))||"diqye".equals(System.getenv("diqye"))){ //如果是本地mysql环境环境
			log.info("初始化 本地mysql 环境 数据库");
			url = Strings.format("jdbc:mysql://{0}:{1}/{2}?useUnicode=true&characterEncoding=utf8","localhost","3306","diqye");
			driverclass= "com.mysql.jdbc.Driver";
			username="root";
			password ="root";
		}
		inited=true;
	}
	public Connection getConnection() throws SQLException {
		try {
			Class.forName(driverclass);
			return DriverManager.getConnection(url, username, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Connection getConnection(String username, String password)
			throws SQLException {
		XAssert.asert("NIY");
		return null;
	}

	public PrintWriter getLogWriter() throws SQLException {
		XAssert.asert("NIY");
		return null;
	}

	public int getLoginTimeout() throws SQLException {
		XAssert.asert("NIY");
		return 0;
	}

	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		XAssert.asert("NIY");
		return null;
	}

	public void setLogWriter(PrintWriter out) throws SQLException {
		XAssert.asert("NIY");

	}

	public void setLoginTimeout(int seconds) throws SQLException {
		XAssert.asert("NIY");

	}

	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		XAssert.asert("NIY");
		return false;
	}

	public <T> T unwrap(Class<T> iface) throws SQLException {
		XAssert.asert("NIY");
		return null;
	}
	
	
	public static void main(String[] args) {
		System.out.println(System.getenv("diqye"));
	}
}
